// pages/charge/charge.js
var formatDate = require('../../fun/formatDate')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    currtab: 'sr', //收入还是支出
    currNavItem: -1, //当前导航
    currPayment: -1, //支付方式
    date: '', //时间
    money: '', //金额
    remarks: '', //备注
    list: [{
        "title": "餐饮",
        "src": "../../images/cy.png",
        "type": "cy",
        "flag":false
      },
      {
        "title": "购物",
        "src": "../../images/gw.png",
        "type": "gw",
        "flag":false
      },
      {
        "title": "出行",
        "src": "../../images/cx.png",
        "type": "cx",
        "flag":false
      },
      {
        "title": "零食",
        "src": "../../images/ls.png",
        "type": "ls",
        "flag":false
      },
      {
        "title": "娱乐",
        "src": "../../images/yl.png",
        "type": "yl",
        "flag":false
      },
      {
        "title": "住房",
        "src": "../../images/zf.png",
        "type": "zf",
        "flag":false
      },
      {
        "title": "交通",
        "src": "../../images/jt.png",
        "type": "jt",
        "flag":false
      },
      {
        "title": "宠物",
        "src": "../../images/cw.png",
        "type": "cw",
        "flag":false
      },
      {
        "title": "学习",
        "src": "../../images/xx.png",
        "type": "xx",
        "flag":false
      },
      {
        "title": "工资",
        "src": "../../images/gz.png",
        "type": "qt",
        "flag":false
      },
      {
        "title": "旅行",
        "src": "../../images/lx.png",
        "type": "qt",
        "flag":false
      },
      {
        "title": "护肤",
        "src": "../../images/hf.png",
        "type": "qt",
        "flag":false
      },
      {
        "title": "电费",
        "src": "../../images/df.png",
        "type": "df",
        "flag":false
      },
      {
        "title": "水费",
        "src": "../../images/sf.png",
        "type": "sf",
        "flag":false
      },
      {
        "title": "办公",
        "src": "../../images/bg.png",
        "type": "bg",
        "flag":false
      },
      {
        "title": "运动",
        "src": "../../images/yd.png",
        "type": "yd",
        "flag":false
      },
      {
        "title": "日用品",
        "src": "../../images/ryp.png",
        "type": "ryp",
        "flag":false
      },
      {
        "title": "社交",
        "src": "../../images/sj.png",
        "type": "sj",
        "flag":false
      },
      {
        "title": "理财",
        "src": "../../images/lc.png",
        "type": "lc",
        "flag":false
      },
      {
        "title": "其他",
        "src": "../../images/qt.png",
        "type": "qt",
        "flag":false
      },
    ], //nav
    Payment_list: [{
        name: '现金',
        type: 'xj',
        color:'#ff6b6b',
      },
      {
        name: '支付宝',
        type: 'zfb',
        color:'#1C77FC',
      },
      {
        name: '微信',
        type: 'wx',
        color:'#00BB1A'
      },
      {
        name: '信用卡',
        type: 'xyk',
        color:'#e6b422'
      },
      {
        name: '储蓄卡',
        type: 'cxk',
        color:'#5B4AC9'
      },
    ], //支付
    // 页数
    currPages:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      date: formatDate(new Date()),
    })


    // 导航项处理
    let i = -1;
    let disposeNavlist=[];
    this.data.list.forEach((item,index)=>{
      if(index%10==0){
          i+=1;
          disposeNavlist[i]=[];
          disposeNavlist[i].push(item);
      }else{
        disposeNavlist[i].push(item);
      }
    })
    this.setData({
      list:disposeNavlist
    })
  },
  //保存支付方式
  payment(e) {
    this.setData({
      currPayment: e.target.dataset.id
    })
  },
  //保存tab是收入还是支出
  toggleTab() {
    this.setData({
      currtab: this.data.currtab == "sr" ? "zc" : 'sr',
    });
  },
  toggleNavItem(e) {
    this.setData({
      currNavItem: e.currentTarget.dataset.i
    })
  //  console.log(this.data.list[this.data.currPages][this.data.currNavItem]);
  },
  swiperChange(e){
    this.setData({
      currNavItem:-1,
      currPages:e.detail.current
    })
  },
  oninput(e) {
    this.setData({
      money: e.detail.value,
    })
    // console.log(e.detail.value);
  },
  ontext(e) {
    this.setData({
      remarks: e.detail.value,
    })
  },
  selectDate(e) {
    this.setData({
      date: e.detail.value
    });
  },
  //保存
  save() {
    if (this.data.currNavItem < 0) {
      wx.showToast({
        title: '类型未选择',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (this.data.currPayment < 0) {
      wx.showToast({
        title: '支付方式未选择',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    if (!this.data.money.length) {
      wx.showToast({
        title: '金额未填写',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    if (isNaN(this.data.money)) {
      wx.showToast({
        title: '金额不能是非数字',
        icon: 'none',
        duration: 2000,
      })
      return
    }
    wx.showLoading({
      title: '提交中',
      mask: true
    })

    let bill = {
      type: this.data.currtab,
      navItem: this.data.list[this.data.currPages][this.data.currNavItem],
      payment: this.data.Payment_list[this.data.currPayment],
      money: this.data.money,
      remarks: this.data.remarks,
      today: this.data.date,
    }

    wx.cloud.callFunction({
      name: 'find_daya',
      data: bill,
    }).then(res => {
      //提交成功关闭loading
      wx.hideLoading({})
      wx.showToast({
        title: '提交成功',
      })
      this.setData({
        money: '',
        remarks: '',
      })
    }).catch(err => {
      console.log("err=>", err);
    })
  },
  //获取用户信息
  bindgetuserinfo(res){
    getApp().globalData.userInfo = res.detail.userInfo;
    getApp().globalData.isAuth = true;
    console.log(getApp().globalData);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})